import { connection } from '../app/database/mysql';
import { getIdGoldShop } from '../goldShop/goldShop.service'
import { getIdHouse} from '../house/house.service'
import { getIdJobs } from '../jobs/jobs.service';
import { getIdCar } from '../car/car.service';
import { getIdSecondHand } from '../secondHand/secondHand.service';
import { getIdPickUp } from '../pickUp/pickUp.service';
import { getIdThrow } from '../throw/throw.service';
import { getIdRepairShop } from '../repairShop/repairShop.service';
import { getIdDelivery } from '../delivery/delivery.service';
import { getIdShop } from '../shop/shop.service';
import { getIdService } from '../service/service.service';
import { getIdInfoField } from '../infoField/infoField.service';
import { getIdMarketPrice } from '../marketPrice/marketPrice.service';
import * as typeNme from '../app/constants';

export const getInfoByIdAndType = async (targetId: number, type: number) => {
  switch (type) {
    case typeNme.IMAGE_TYPE_GOLDSHOP: return getIdGoldShop(targetId);
    case typeNme.IMAGE_TYPE_HOUSE: return getIdHouse(targetId);
    case typeNme.IMAGE_TYPE_JOBS: return getIdJobs(targetId);
    case typeNme.IMAGE_TYPE_CAR: return getIdCar(targetId);
    case typeNme.IMAGE_TYPE_SECONDHAND: return getIdSecondHand(targetId);
    case typeNme.IMAGE_TYPE_PICKEDUP: return getIdPickUp(targetId);
    case typeNme.IMAGE_TYPE_THROW: return getIdThrow(targetId);
    case typeNme.IMAGE_TYPE_MAINTENANCE: return getIdRepairShop(targetId);
    case typeNme.IMAGE_TYPE_RUNERRANDS: return getIdDelivery(targetId);
    case typeNme.IMAGE_TYPE_SHOP: return getIdShop(targetId);
    case typeNme.IMAGE_TYPE_SERVISE: return getIdService(targetId);
    case typeNme.IMAGE_TYPE_SENDINFO: return getIdInfoField(targetId);
    case typeNme.IMAGE_TYPE_MARKETPRICE: return getIdMarketPrice(targetId);
  }
}

export const saveCorner = async (targetId: number, type: number,userId: number,status:number) =>{
  // 准备查询
  const statement = `
    INSERT INTO tb_index
    SET ?
  `;
  let corner = {
    target_id:targetId,
    type: type,
    user_id: userId,
    status: status,
  }

  // 执行查询
  const [data] = await connection.promise().query(statement, corner);

  // 提供数据
  return data;
}

export const cornerStatusChange = async (postId: number, post: any) => {
  // 准备数据
  const statement = `
    UPDATE tb_index
      SET ?
      WHERE id = ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, [post, postId]);
  // 提供数据
  return data;
};

/**
 * 
 */
export const cornerStatusChangeByType = async (targetId: number, type: number,status: number)=> {
  // 准备数据
  const statement = `
    UPDATE tb_index
      SET status = ?
      WHERE target_id = ? and type = ? 
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, [status,targetId,type]);
  
  // 提供数据
  return data;
};

export const cornerDestroyById = async (postId: number, type: number) => {
  // 准备数据
  const statement = `
    DELETE FROM tb_index
    WHERE target_id = ?
    AND type = ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, [postId, type]);

  // 提供数据
  return data;
}
